use Thomson_Ch4_Data.dta, clear
set more off
set scheme s2mono

*********************
* Time Series Plots *
*********************
gen net_policy = foodindex*(1+cte_covt)
label var net_policy "Index*CTE"
label var year "Year"

graph twoway (connected cte_covt year if country=="Indonesia" & year>=1970 & year<2009, yaxis(2) lwidth(medthin) msymbol(oh) lpattern(longdash) lcolor(gs9)) ///
(connected foodindex year if country=="Indonesia" & year>=1970, lwidth(medium) msymbol(dh) msize(small) lpattern(solid) lcolor(gs0) ///
title("Indonesia") graphregion(color(white)) xsize(6) ysize(5))
graph save indonesia1, replace

graph twoway (connected foodindex year if country=="Indonesia" & year>=1970 & year<2009, lwidth(medium) msymbol(dh) lpattern(solid) lcolor(gs0)) ///
(connected net_policy year if country=="Indonesia" & year>=1970, ylabel(40(80)200) lwidth(medthin) msymbol(th) msize(small) lpattern(longdash) lcolor(gs9) ///
title("Indonesia") graphregion(color(white))) ///
(spike nevent year if cityname=="Jakarta" & year>=1970, lwidth(medthick) yaxis(2) lpattern(solid) graphregion(color(white)) xsize(6) ysize(5))
graph save indonesia2, replace

graph twoway (connected cte_covt year if country=="Thailand" & year>=1970 & year<2009, yaxis(2) lwidth(longdash) msymbol(oh) lpattern(solid) lcolor(gs9)) ///
(connected foodindex year if country=="Thailand" & year>=1970, lwidth(medium) msymbol(dh) msize(small) lpattern(solid) lcolor(gs0) ///
title("Thailand") graphregion(color(white)) xsize(6) ysize(5))
graph save thailand1, replace

graph twoway (connected foodindex year if country=="Thailand" & year>=1970 & year<2009, lwidth(medium) msymbol(dh) lpattern(solid) lcolor(gs0)) ///
(connected net_policy year if country=="Thailand" & year>=1970, ylabel(40(80)200) lwidth(medthin) msymbol(th) msize(small) lpattern(longdash) lcolor(gs9) ///
title("Thailand") graphregion(color(white))) ///
(spike nevent year if cityname=="Bangkok" & year>=1970, lwidth(medthick) yaxis(2) lpattern(solid) graphregion(color(white)) xsize(6) ysize(5))
graph save thailand2, replace

grc1leg indonesia1.gph thailand1.gph, cols(2) graphregion(color(white)) ycommon xsize(7) ysize(5)
*graph export Indonesia_Thailand_Graphs_1.pdf, replace

grc1leg indonesia2.gph thailand2.gph, cols(2) graphregion(color(white)) ycommon xsize(7) ysize(5)
*graph export Indonesia_Thailand_Graphs_2.pdf, replace


corr nevent nodeathevent expindex cte_covt foodindex



*********************
* Correlation Plots *
*********************
xtnbreg nevent cte_covt democracy anocracy foodindex ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe

corr cte_covt nevent rgdppcln expindex lncitypop foodindex if e(sample)

graph twoway (scatter nevent cte_covt if e(sample), msymbol(Oh)) ///
(lfit nevent cte_covt, lpattern(solid) lwidth(medthick)) if e(sample), ///
xtitle("Consumer Tax") ytitle("Unrest Events") title("CTE v Unrest") legend(off) xsize(4) ysize(4) graphregion(color(white)) bgcolor(white)
graph save corr_cte_unrest, replace

graph twoway (scatter cte_covt rgdppcln if e(sample), msymbol(Oh)) ///
(lfit cte_covt rgdppcln, lpattern(solid) lwidth(medthick)) if e(sample), ///
ytitle("Consumer Tax") xtitle("Ln GDP") title("GDP v CTE") legend(off) xsize(4) ysize(4) graphregion(color(white)) bgcolor(white)
graph save corr_cte_gdp, replace

graph twoway (scatter nevent lncitypop if e(sample), msymbol(Oh)) ///
(lfit nevent lncitypop, lpattern(solid) lwidth(medthick)) if e(sample), ///
xtitle("Log City Pop") ytitle("Unrest Events") title("City Size v Unrest") legend(off) xsize(4) ysize(4) graphregion(color(white)) bgcolor(white)
graph save corr_citypop_unrest, replace

graph twoway (scatter cte_covt expindex if e(sample), msymbol(Oh)) ///
(lfit cte_covt expindex, lpattern(solid) lwidth(medthick)) if e(sample), ///
ytitle("Consumer Tax") xtitle("Export Index") title("Exp Index v CTE") legend(off) xsize(4) ysize(4) graphregion(color(white)) bgcolor(white)
graph save corr_cte_expdindex, replace


graph combine corr_cte_unrest.gph corr_cte_expdindex.gph corr_citypop_unrest.gph corr_cte_gdp.gph, cols(2) graphregion(color(white))
*graph export Unrest_Scatterplots.pdf, replace


* Summary stats of variables
sutex nevent nodeathevent unrest_dummy cte_covt expindex anocracy democracy foodindex rgdppcln lncitypop citygrowth5 conflict endcoldwar econshock timetrend  if e(sample), ///
file ("Var_Stats.tex") labels minmax dig(2) title("Summary Statistics: Unrest Models") key(tab:var_stats_unrest) replace

corrtex nevent nodeathevent expindex cte_covt foodindex if e(sample), ///
file ("Corr_Tab.tex") dig(2) title("Correlation Table") key(tab:corr_tab) replace noscreen




********************************
* Consumer Taxes Unconditional *
********************************
* Model 1.1 NBREG, FEs, lagged IVs
xtnbreg nevent l_cte democracy anocracy l_foodindex ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe
est store xtnb_cte
margins, at(l_cte=(-.34(0.1).37) anocracy=1 democracy=0 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("Model 1.1") ytitle("Unrest") xtitle("CTE") graphregion(color(white)) bgcolor(white) xsize(6) ysize(5) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) yline(0, lwidth(thin)) ///
legend(off)
graph save xtnb_cte, replace

margins, at(l_foodindex=(64(10)114) anocracy=1 democracy=0 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)

xtnbreg nevent l_cte l_foodindex i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe
margins, dydx(regime)
margins, at(lncitypop=(6.6 7.7 8.76) regime=1 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)

*(Check a Poisson model also)
poisson nevent l_cte l_foodindex i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend i.cityid
margins, at(l_cte=(-.34(0.1).37) regime==1 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)


* Model 1.2 NBREG, REs, lagged DV and IVs, clustered SEs
nbreg nevent l_cte democracy anocracy l_foodindex ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend l_nevent, vce(cluster cityid)
est store xtnb_cte_re


* Model 1.3 Logit, FEs, lagged IVs
logit unrest_dummy l_cte democracy anocracy l_foodindex ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend i.cityid
est store logit_cte

margins, at(l_cte=(-.34(0.1).37) anocracy=1 democracy=0 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("Model 1.3") ytitle("Pr(Unrest 0/1)") xtitle("CTE") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) yline(0, lwidth(thin)) ///
legend(off)
graph save logit_cte, replace


* Model 1.4 Inst Var Reg
ivregress 2sls nevent anocracy democracy l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend (l_cte =  l_expindex), first vce(cluster cityid)
est store cte_ivreg

margins, at(l_cte=(-.34(0.1).37) anocracy=1 democracy=0 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)


* Model 1.5 Inst Var Poisson
ivpoisson gmm nevent l_foodindex anocracy democracy rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend (l_cte =  l_expindex),  vce(cluster cityid)
est store cte_ivpois

margins, at(l_cte=(-.34(0.1).37) anocracy=1 democracy=0 conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("Model 1.5") ytitle("Unrest") xtitle("CTE") graphregion(color(white)) bgcolor(white) xsize(6) ysize(5) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) ///
legend(off)
graph save cte_ivpois, replace


* Tables and graphs of results -- unconditional relationship
esttab xtnb_cte xtnb_cte_re logit_cte cte_ivreg cte_ivpois, ///
keep(l_cte l_foodindex democracy anocracy econshock irreg_exit regchange lncitypop conflict) ///
order(l_cte l_foodindex democracy anocracy econshock irreg_exit regchange conflict lncitypop) ///
mtitle("NB, FEs" "NB, REs" "Logit" "IV Reg" "IV Pois") ///
label b(3) se compress nogaps ///
addnote("Autocracy is the base category." "All models control for GDP, time trend, end of CW, elections.")

esttab xtnb_cte xtnb_cte_re logit_cte cte_ivreg cte_ivpois using CTE_Uncond_Models.tex, ///
keep(l_cte l_foodindex democracy anocracy econshock irreg_exit regchange lncitypop conflict ) ///
order(l_cte l_foodindex democracy anocracy econshock irreg_exit regchange conflict lncitypop) ///
mtitle("NB, FEs" "NB, REs" "Logit" "IV Reg" "IV Pois") ///
label b(3) se compress nogaps replace ///
addnote("Autocracy is the base category." "All models control for GDP, time trend, end of CW.")

grc1leg xtnb_cte.gph cte_ivpois.gph, cols(2) graphregion(color(white))
*graph export "CTE_Uncond_Comb.pdf", replace



*********************************************
* Consumer Taxes Conditional on Regime Type *
*********************************************
*NBREG, FEs, lagged IVs
xtnbreg nevent l_cte l_foodindex democracy anocracy ctel_democ ctel_anoc foodindexl_democ foodindexl_anoc ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe
est store xtnb_cte_regime

xtnbreg nevent c.L1.cte_covt##i.regime c.L1.foodindex##i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe

margins i.regime, at(c.L1.cte_covt=(-.35(0.1).35) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recastci(rline) ///
plot1opts(lwidth(thin) lpattern(solid) lcolor(gs12) msymbol(oh) mcolor(gs12)) ///
ci1opts(lwidth(vthin) lpattern(longdash) lcolor(gs12)) ///
plot2opts(lwidth(med) lpattern(solid) msymbol(Dh) mcolor(black)) ///
ci2opts(lwidth(thin) lpattern(longdash) lcolor(black)) ///
plot3opts(lwidth(thin) lpattern(solid) lcolor(gs8) msymbol(sh) mcolor(gs8)) ///
ci3opts(lwidth(vthin) lpattern(longdash)  lcolor(gs8)) ///
title("Effect of Regime by CTE") ytitle("Effect on Unrest") xtitle("Consumer Taxes (CTE)") ///
addplot(kdensity cte_covt if cte_covt>-.35 & cte_covt<.35 & e(sample), yaxis(2) xlabel(-0.35(0.175)0.35)) yline(0, lwidth(vthin)) ///
legend(pos(6) cols(2) order(3 2 1) size(small)) xsize(7) ysize(5) graphregion(color(white)) bgcolor(white) ylabel(-0.8(0.4)0.8)
graph save xtnb_cte_regime_1, replace

margins, dydx(c.L1.cte_covt) at(regime=(0 1 2))
marginsplot, recast(scatter) ///
plot1opts(msymbol(Oh) msize(large) mlwidth(medthick)) ///
ci1opts(lwidth(medthick)) ///
yline(0, lwidth(thin)) xsize(5) ysize(6) ///
xlabel(0 "Autoc" 1 "Anoc" 2 "Democ") xtitle("") title("Effects of CTE by Regime") ytitle("Effect on Unrest") graphregion(color(white))
graph save xtnb_cte_regime_2, replace

graph combine xtnb_cte_regime_2.gph xtnb_cte_regime_1.gph, cols(2) graphregion(color(white))
*graph export CTE_Regime_Model1_Comb.pdf, replace

margins regime, at(c.L1.foodindex=(62(10)113)) 


*NBREG, REs, lagged DV and IVs
	nbreg nevent l_cte l_foodindex democracy anocracy ctel_democ ctel_anoc foodindexl_democ foodindexl_anoc ///
	rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend l_nevent, vce(cluster cityid)
	est store xtnb_cte_regime_re

nbreg nevent c.L1.cte_covt##i.regime c.L1.foodindex##i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend l_nevent, vce(cluster cityid)

margins i.regime, at(c.L1.cte_covt=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
margins, dydx(c.L1.cte_covt) at(regime=(0 1 2))


* Logit, FEs, lagged IVs
logit unrest_dummy l_cte l_foodindex democracy anocracy ctel_democ ctel_anoc foodindexl_democ foodindexl_anoc ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend i.cityid
est store logit_cte_regime

logit unrest_dummy c.L1.cte_covt##i.regime c.L1.foodindex##i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend i.cityid
margins i.regime, at(c.L1.cte_covt=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recastci(rline) ci1opts(lwidth(thin) lpattern(longdash)) ci2opts(lwidth(thin) lpattern(dash) lcolor(black)) ci3opts(lwidth(thin) lpattern(longdash_dot)) ///
title("Probability of Unrest") ytitle("Pr(Unrest 0/1)") xtitle("CTE") ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) ///
legend(pos(6) cols(2) order(3 2 1) size(small)) xsize(5) ysize(7) graphregion(color(white)) bgcolor(white)
graph save logit_cte_regime, replace

margins, dydx(c.L1.cte_covt) at(regime=(0 1 2))
marginsplot, recast(scatter) yline(0, lwidth(thin)) xsize(5) ysize(6) ///
xlabel(0 "Autoc" 1 "Anoc" 2 "Democ") xtitle("") title("Effects of CTE by Regime") ytitle("Effect on Unrest")
graph save logit_cte_regime_2, replace


* 3-way interaction of policy, food index and regime type for interest/robustness
xtnbreg nevent c.cte_covt##c.foodindex##i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe
margins i.regime, at(c.cte_covt=(-.34(0.1).37))

* With lags
xtnbreg nevent c.L1.cte_covt##c.L1.foodindex##i.regime ///
rgdppcln lncitypop conflict irreg_exit regchange election citygrowth5 endcoldwar econshock timetrend, fe
margins i.regime, at(c.L1.cte_covt=(-.34(0.1).37))
marginsplot, title("Unrest") ytitle("") xtitle("CTE") ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.1)0.37)) yline(0, lwidth(thin)) ///
legend(pos(6) cols(2) order(3 2 1) size(small)) graphregion(color(white))


* IV Models -- ANOCRACY
* Linear
ivregress 2sls nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if anocracy==1, first vce(cluster cityid)
est store cte_ivreg_anoc

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)

*Poisson
ivpoisson gmm nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if anocracy==1,  vce(cluster cityid)
est store cte_ivpois_anoc

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("IVP Anoc") ytitle("Unrest") xtitle("CTE") graphregion(color(white)) bgcolor(white) xsize(7) ysize(5) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) ///
legend(off)
graph save cte_ivpois_anoc, replace


* IV Models -- DEMOCRACY
* Linear
ivregress 2sls nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if democracy==1, first vce(cluster cityid)
est store cte_ivreg_democ

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)

*Poisson
ivpoisson gmm nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if democracy==1,  vce(cluster cityid)
est store cte_ivpois_democ

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("IVP Democ") ytitle("Unrest") xtitle("CTE") graphregion(color(white)) bgcolor(white) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) ///
legend(off)
graph save cte_ivpois_democ, replace

* IV Models -- AUTOCRACY
* Linear
ivregress 2sls nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if autocracy==1, first vce(cluster cityid)
est store cte_ivreg_autoc

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)

*Poisson
ivpoisson gmm nevent l_foodindex rgdppcln lncitypop conflict irreg_exit regchange election endcoldwar citygrowth5 econshock l_nevent timetrend ///
(l_cte =  l_expindex) if autocracy==1,  vce(cluster cityid)
est store cte_ivpois_autoc

margins, at(l_cte=(-.34(0.1).37) conflict=0 irreg_exit=0 regchange=0 election=0 endcoldwar=0 econshock=0)
marginsplot, recast(line) recastci(rline) plot1opts(lwidth(medium)) ciopts(lwidth(thin) lpattern(longdash)) ///
title("IVP Autoc") ytitle("Unrest") xtitle("CTE") graphregion(color(white)) bgcolor(white) xsize(7) ysize(5) ///
addplot(kdensity cte_covt if cte_covt>-.34 & cte_covt<.37 & e(sample), yaxis(2) xlabel(-0.34(0.2)0.37)) ///
legend(off)	
graph save cte_ivpois_autoc, replace


* Tables and graphs of results -- conditional relationship
esttab xtnb_cte_regime xtnb_cte_regime_re logit_cte_regime cte_ivreg_anoc cte_ivpois_anoc cte_ivpois_democ cte_ivpois_autoc, ///
keep(l_cte l_foodindex ctel_anoc ctel_democ foodindexl_anoc foodindexl_democ anocracy democracy  econshock irreg_exit) ///
order(l_cte l_foodindex ctel_democ ctel_anoc foodindexl_democ foodindexl_anoc anocracy democracy  econshock irreg_exit) ///
mtitle("NB, FEs" "NB, REs" "Logit" "IVR Anoc" "IVP Anoc" "IVP Democ" "IVP Autoc") ///
label b(3) se compress nogaps ///
addnote("Autocracy is the base category. All models with full set of controls.")

esttab xtnb_cte_regime xtnb_cte_regime_re logit_cte_regime cte_ivreg_anoc cte_ivpois_anoc cte_ivpois_democ cte_ivpois_autoc ///
using CTE_Cond_Models.tex, ///
keep(l_cte l_foodindex ctel_anoc ctel_democ foodindexl_anoc foodindexl_democ anocracy democracy  econshock irreg_exit) ///
order(l_cte l_foodindex ctel_democ ctel_anoc foodindexl_democ foodindexl_anoc anocracy democracy  econshock irreg_exit) ///
mtitle("NB, FEs" "NB, REs" "Logit" "IVR Anoc" "IVP Anoc" "IVP Democ" "IVP Autoc") ///
label b(3) se compress nogaps replace ///
addnote("Autocracy is the base category. All models with full set of controls.")

graph combine cte_ivpois_anoc.gph cte_ivpois_autoc.gph, cols(2) graphregion(color(white))
*graph export "CTE_Cond_IV.pdf", replace

